package org.leumasjaffe.math.probability;

import java.util.Random;

import org.eclipse.jdt.annotation.NonNull;

import flanagan.analysis.Stat;


public class LogNormalRV extends AbstractRV {
	final double mu;
	final double sig2;

	public LogNormalRV(double scale, double sig2) {
		this.mu = scale;
		this.sig2 = sig2;
	}

	public LogNormalRV(@NonNull Random r, double scale, double sig2) {
		super(r);
		this.mu = scale;
		this.sig2 = sig2;
	}

	@Override
	public double getEV() {
		return Math.exp(this.mu+this.sig2/2);
	}

	@Override
	public double getVar() {
		return (Math.exp(this.sig2)-1)*Math.exp(2*this.mu+this.sig2);
	}

	@Override
	public double getP(double x) {
		return 1/(x*Math.sqrt(2*Math.PI)*this.sig2)*Math.exp(((Math.log(x)-this.mu)*(Math.log(x)-this.mu))/
				(2*this.sig2));
	}

	@Override
	public double calcX(double p) {
		if (!isProb(p)) throw new IllegalArgumentException("Arguement is not a valid probability");
		return Stat.logNormalInverseCDF(this.mu, this.sig2, p);
	}

	@Override
	public double getF(double x) {
		return 0.5 + 0.5*Stat.erf(Math.log(x)-getEV()/Math.sqrt(2*this.sig2));
	}

}
